# category_page.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class CategoryPage:
    # 初始化 CategoryPage 对象
    def __init__(self, driver):
        self.driver = driver
        self.url = 'http://example.com/categories'
        self.category_title_locator = (By.XPATH, '//h1[@class="category-title"]')
        self.category_items_locator = (By.CSS_SELECTOR, '.category-items .item')
        self.sort_by_selector_locator = (By.ID, 'sort-by-selector')
        self.filter_by_price_slider_locator = (By.ID, 'filter-price-slider')
        self.show_more_button_locator = (By.ID, 'show-more-button')

    # 导航至商品分类页面
    def navigate(self):
        self.driver.get(self.url)

    # 获取商品分类页面的标题
    def get_category_title(self):
        return self.driver.find_element(*self.category_title_locator).text

    # 获取商品分类页面上的所有商品信息
    def get_category_items(self):
        return [item.text for item in self.driver.find_elements(*self.category_items_locator)]

    # 选择商品排序选项
    def select_sort_option(self, option):
        sort_by_selector = self.driver.find_element(*self.sort_by_selector_locator)
        sort_by_selector.send_keys(option)

    # 设置价格过滤器
    def set_price_filter(self, min_price, max_price):
        filter_slider = self.driver.find_element(*self.filter_by_price_slider_locator)
        # 假设有一个方法来设置滑块的位置
        filter_slider.set_min_value(min_price)
        filter_slider.set_max_value(max_price)

    # 点击“显示更多”按钮
    def click_show_more(self):
        show_more_button = self.driver.find_element(*self.show_more_button_locator)
        show_more_button.click()

    # 等待商品加载完成
    def wait_for_item_to_load(self):
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.category_items_locator)
        )